<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            text-align: center;
        }

        .a {
            height: 200px;
            background-color: green;
        }

        .b {
            height: 50px;
            background-color: red;
        }

        .c {
            height: 2000px;
            background-color: gray;
        }

        .d {
            height: 50px;
            background-color: yellow;
        }

        .active {
            width: 100%;
            top: 0;
            position: fixed;
        }
    </style>
</head>

<body>
    <div class="a">轮播</div>
    <div class="b">导航</div>
    <div class="c">内容</div>
    <div class="d">底部</div>

    <script>
        const oB = document.querySelector('.b');
        // 拿到导航栏距离顶部的距离
        const oTop = oB.offsetTop;
        console.log(oTop);
        // 滚动事件
        document.body.onscroll = function () {
            // 拿到滚动的距离
            const scrollTop = document.documentElement.scrollTop;
            console.log(scrollTop);
            //判断
            if (scrollTop >= oTop) {
                oB.classList.add('active');
            } else {
                oB.classList.remove('active');
            }
        }
    </script>
</body>

</html>